package Be_greedy_for_snakes;

import java.util.LinkedList;             //蛇由多个节点组成，用LinkdeList集合储存Node节点

public class Snake {
    private LinkedList<Node> body;
    private Direction direction = Direction.LEFT;//初始方向向左移动，若要改变移动方向，只需修改Direction.()即可
    private boolean Living = true;               //用布尔变量来判断蛇的存活
    private int score=0;                   //分数

    public Snake() {                   //构造方法
        initSnake();                    //初始化
    }

    private void initSnake() {                  //方法
        body = new LinkedList<>();              //创建集合
        //将节点添加到集合中；并调整蛇身初始位置
        body.add(new Node(23,17));
        body.add(new Node(24,17));
        body.add(new Node(25,17));
        body.add(new Node(26,17));
        body.add(new Node(27,17));
        body.add(new Node(28,17));
    }

    public void move(){                         //蛇的运动方向
        if(Living){                               //蛇存活才执行操作
            Node first = body.getFirst();         //得到蛇头节点
            switch(direction){
                case UP:             //用addFirst来移动蛇头
                    body.addFirst(new Node(first.getX(),first.getY()-1));
                    break;
                case DOWN:
                    body.addFirst(new Node(first.getX(),first.getY()+1));
                    break;
                case LEFT:
                    body.addFirst(new Node(first.getX()-1,first.getY()));
                    break;
                case RIGHT:
                    body.addFirst(new Node(first.getX()+1,first.getY()));
                    break;
            }
            body.removeLast();//删除蛇尾节点
            first = body.getFirst();
            //若蛇触碰到边线则死亡
            if(first.getX()<0||first.getY()<0||first.getX()>=50||first.getY()>=35){
                Living = false;
            }

            for (int i = 1; i < body.size(); i++) {
                Node node = body.get(i);
                //判断蛇头节点是否碰到身体节点
                if(first.getX()==node.getX()&&first.getY()==node.getY()){
                    Living = false;
                }
            }
        }
    }

    public LinkedList<Node> getBody() {
        return body;
    }

    public void setBody(LinkedList<Node> body) {
        this.body = body;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public boolean isLiving() {
        return Living;
    }

    public void setLiving(boolean living) {
        Living = living;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void eat(Node food) {
        Node first = body.getFirst();
        switch(direction){
            case UP:             //用addFirst来移动蛇头
                body.addFirst(new Node(first.getX(),first.getY()-1));
                break;
            case DOWN:
                body.addFirst(new Node(first.getX(),first.getY()+1));
                break;
            case LEFT:
                body.addFirst(new Node(first.getX()-1,first.getY()));
                break;
            case RIGHT:
                body.addFirst(new Node(first.getX()+1,first.getY()));
                break;
        }
    }
}
